************************************
************************************
* Descriptives
************************************
************************************

use "${FINALDATA}\immo.dta", replace

*************************************************
* generate codebook
************************************************* 
if ($LOGTIME == 1) log using "${LOG}/codebook_immo.log", replace
	codebook, compact 
log close


*************************************************
* generate and recode variables
************************************************* 
gen multlist = infocount > 1 & !missing(infocount)
distinct geo if multlist == 1

** generate variables to avoid factors 
codebook objektkat
gen rowhouse = objektkat == 1 if objektkat != 99
gen mfh = objektkat == 2 if objektkat != 99
gen efm = objektkat == 3 if objektkat != 99

codebook qualitaet
gen qu_superior = qualitaet == 3 if qualitaet != 99
gen qu_normal = qualitaet == 4 if qualitaet != 99
gen qu_simple = qualitaet == 5 if qualitaet != 99

codebook objektzus
gen zus_renov = objektzus == 0 if objektzus!= 99
gen zus_good = objektzus == 1 if objektzus!= 99

codebook heizart
gen h_central = heizart == 1 if heizart!=99
gen h_etage = heizart == 2 if heizart!=99
gen h_oven = heizart == 3 if heizart!=99

codebook bstoff
recode bstoff (3 4 5 6  = 10)
gen b_oil = bstoff == 1 if bstoff!=99
gen b_gas = bstoff == 2 if bstoff!=99
gen b_elec = bstoff == 7 if bstoff!=99
gen b_others = bstoff == 10 if bstoff!=99


cap log close
if ($LOGTIME == 1) log using "${LOG}/${DATE}_descriptive_statistics.log", replace


************** 
**************
* Descriptive Statistics
**************
**************

* recode variables
replace kaufpreis = kaufpreis/1000
replace rentner_anteil_plz = rentner_anteil/100
replace auslaender_anteil_plz = auslaender_anteil_plz/100



** Table 2 (descriptives for EPC = 1 und EPC = 0)
*********************************************************
global descvars_results kaufpreis duration wohnflaeche grundflaeche zimmer_cont alter modernvor mfh efm rowhouse qu_simple qu_normal qu_superior zus_renov zus_good h_central h_etage h_oven b_oil b_gas b_elec b_others banks eagents private evk


* EPC = 1
quietly tabstat $descvars_results if epc == 1, save stats(mean sd N)

mat aux = r(StatTotal)
mat results = aux'
matrix rownames results = $descvars_results
matrix colnames results = mean sd n
matrix together = results

cap matrix drop results aux
cap matrix drop export 

* EPC = 0
quietly tabstat $descvars_results if epc == 0, save stats(mean sd N)

mat aux = r(StatTotal)
mat results = aux'
matrix rownames results = $descvars_results
matrix colnames results = mean sd n
matrix together = together, results

cap matrix drop results aux
cap matrix drop export 
 

** export to latex
estout matrix(together, fmt( ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc" ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc" ///
	"%11.0fc " ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc" ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc" ///
	"%11.0fc " ///	
)), // style(tex)

 
 
 

** Table 3 (Online Appendix E) 
******************************************************************
 
global descvars_results kaufpreis duration wohnflaeche grundflaeche zimmer_cont alter modernvor mfh efm rowhouse qu_simple qu_normal qu_superior zus_renov zus_good h_central h_etage h_oven b_oil b_gas b_elec b_others banks eagents private epc evk
global descvars_regional dichte_plz kaufkraft_pk_plz rentner_anteil_plz auslaender_anteil_plz


* Multlist = 1
quietly tabstat $descvars_results $descvars_regional if multlist == 1, save stats(mean sd N)

mat aux = r(StatTotal)
mat results = aux'
matrix rownames results = $descvars_results $descvars_regional
matrix colnames results = mean sd n
matrix together = results
	
cap matrix drop results aux
cap matrix drop export 


* Multlist = 0
quietly tabstat $descvars_results $descvars_regional if multlist == 0, save stats(mean sd N)

mat aux = r(StatTotal)
mat results = aux'
matrix rownames results = $descvars_results $descvars_regional
matrix colnames results = mean sd n
matrix together = together, results


** Further statistics (T-Tests and standardized bias)

scalar rowcount = wordcount("$descvars_results $descvars_regional")
mat statistic = J(rowcount,1,.)

local count = 1
foreach var of varlist $descvars_results $descvars_regional {
	* normalized difference
	quietly sum `var' if multlist == 1
	scalar mean1 = r(mean)
	scalar sd1 = r(sd)
	quietly sum `var' if multlist == 0
	scalar mean0 = r(mean)
	scalar sd0 = r(sd)
	
	mat statistic[`count',1] =  ((mean1 - mean0)/(sqrt((sd1^2+sd0^2)/2)))*100
	matrix rownames statistic = $descvars_results $descvars_regional
	local count = `count' + 1
}

matrix together = together, statistic

** export to latex
estout matrix(together, fmt( ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc %11.1fc %11.1fc 2 2" ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc %11.1fc %11.1fc 2 2" ///
	"%11.0fc " ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc %11.1fc %11.1fc 2 2" ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc %11.1fc %11.1fc 2 2" ///
	"%11.0fc " ///
	"%11.1fc " ///	
)), // style(tex)



** Table 4 (Online Appendix F): Number of Sales on Immobilienscout in 2013 and 2015 across Seller Types
***************************************************	

bysort anid: egen objekts_anid = count(anid)
egen tag_anid = tag(anid)

foreach agent in banks eagents private {
	tabstat objekts_anid if tag_anid & `agent', stats(sum N mean sd p10 p90) save
	mat `agent'_results = r(StatTotal)
}

mat results = (banks_results, eagents_results, private_results)	
mat colnames results = banks eagents private	
	
	
estout matrix(results, fmt( ///
	"%11.0fc %11.0fc %11.1fc %11.1fc %11.0fc %11.0fc" ///
	"%11.0fc %11.0fc %11.1fc %11.1fc %11.0fc %11.0fc" ///
	"%11.0fc %11.0fc %11.1fc %11.1fc %11.0fc %11.0fc" ///
)), // style(tex)

	
	
** Table 5 (Online Appendix F): Differences in Characteristics, by Seller Type
******************************************************************	
global descvars_results kaufpreis duration wohnflaeche grundflaeche zimmer_cont alter modernvor mfh efm rowhouse qu_simple qu_normal qu_superior zus_renov zus_good h_central h_etage h_oven b_oil b_gas b_elec b_others epc evk
global descvars_regional dichte_plz kaufkraft_pk_plz rentner_anteil_plz auslaender_anteil_plz
	
	
** banks
quietly tabstat $descvars_results $descvars_regional if banks == 1, save stats(mean)

mat aux = r(StatTotal)
mat results = aux'
matrix rownames results = $descvars_results $descvars_regional
matrix colnames results = mean
matrix together = results

cap matrix drop results aux
cap matrix drop export 
	
	
** eagents
quietly tabstat $descvars_results $descvars_regional if eagents == 1, save stats(mean)

mat aux = r(StatTotal)
mat results = aux'
matrix rownames results = $descvars_results $descvars_regional
matrix colnames results = mean
matrix together = together, results
	
cap matrix drop results aux
cap matrix drop export 
	

** private
quietly tabstat $descvars_results $descvars_regional if private == 1, save stats(mean)

mat aux = r(StatTotal)
mat results = aux'
matrix rownames results = $descvars_results $descvars_regional
matrix colnames results = mean
matrix together = together, results

cap matrix drop results aux
cap matrix drop export 
	
		
** Further statistics: Standardized Bias banks eagents

scalar rowcount = wordcount("$descvars_results $descvars_regional")
mat statistic = J(rowcount,1,.)

local count = 1
foreach var of varlist $descvars_results $descvars_regional {

	* normalized difference
	quietly sum `var' if banks == 1
	scalar mean1 = r(mean)
	scalar sd1 = r(sd)
	quietly sum `var' if eagents == 1
	scalar mean0 = r(mean)
	scalar sd0 = r(sd)
	
	mat statistic[`count',1] =  ((mean1 - mean0)/(sqrt((sd1^2+sd0^2)/2)))*100
	matrix rownames statistic = $descvars_results $descvars_regional
	local count = `count' + 1
}

matrix together = together, statistic

** Further statistics: Standardized Bias banks eagents

scalar rowcount = wordcount("$descvars_results $descvars_regional")
mat statistic = J(rowcount,1,.)

local count = 1
foreach var of varlist $descvars_results $descvars_regional {
	* normalized difference
	quietly sum `var' if eagents == 1
	scalar mean1 = r(mean)
	scalar sd1 = r(sd)
	quietly sum `var' if private == 1
	scalar mean0 = r(mean)
	scalar sd0 = r(sd)
	
	mat statistic[`count',1] =  ((mean1 - mean0)/(sqrt((sd1^2+sd0^2)/2)))*100
	matrix rownames statistic = $descvars_results $descvars_regional
	local count = `count' + 1
}

matrix together = together, statistic

estout matrix(together, fmt( ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc %11.1fc %11.1fc 2 2" ///
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc %11.1fc %11.1fc 2 2" ///	
	"%11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc %11.1fc 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 %11.1fc %11.1fc %11.1fc 2 2" ///	
	%11.1fc ///
	%11.1fc ///
	)), // style(tex)


if ($LOGTIME == 1) log close	

